package com.ruoyi.resume.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.resume.domain.model.Category;
import com.ruoyi.resume.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 分类管理控制器
 */
@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category 分类对象，包含分类的详细信息
     * @return 响应结果字符串，包含创建成功信息或失败原因
     */
    @ApiOperation(value = "新增分类", notes = "创建一个新的分类，根据传入的分类对象")
    @PostMapping("/create-category")
    @Anonymous
    public ResponseEntity<String> createCategory(
            @ApiParam(value = "分类对象", required = true) @RequestBody Category category) {
        Integer newId = categoryService.createCategory(category);
        if (newId != null) {
            return ResponseEntity.ok("分类创建成功, id: " + newId);
        } else {
            return ResponseEntity.status(500).body("分类创建失败");
        }
    }

    /**
     * 删除分类
     * @param id 分类ID
     * @return 响应结果字符串，包含删除成功信息或失败原因
     */
    @ApiOperation(value = "删除分类", notes = "根据分类ID删除对应的分类")
    @DeleteMapping("/delete-category/{id}")
    @Anonymous
    public ResponseEntity<String> deleteCategoryById(
            @ApiParam(value = "分类ID", required = true) @PathVariable Integer id) {
        boolean isDeleted = categoryService.deleteCategoryById(id);
        if (isDeleted) {
            return ResponseEntity.ok("分类删除成功");
        } else {
            return ResponseEntity.status(404).body("分类不存在或删除失败");
        }
    }

    /**
     * 修改分类
     * @param category 分类对象，包含需要修改的分类详细信息
     * @return 响应结果字符串，包含修改成功信息或失败原因
     */
    @ApiOperation(value = "修改分类", notes = "根据传入的分类对象更新分类信息")
    @PutMapping("/update-category")
    @Anonymous
    public ResponseEntity<String> updateCategory(
            @ApiParam(value = "分类对象", required = true) @RequestBody Category category) {
        boolean isUpdated = categoryService.updateCategory(category);
        if (isUpdated) {
            return ResponseEntity.ok("分类修改成功");
        } else {
            return ResponseEntity.status(404).body("分类不存在或修改失败");
        }
    }
}
